package com.lagou.service.impl;

import com.lagou.base.StatusCode;
import com.lagou.dao.CourseDao;
import com.lagou.dao.impl.CourseDaoImpl;
import com.lagou.poji.Course;
import com.lagou.service.CourseService;
import com.lagou.utils.DateUtils;
import com.lagou.utils.DruidUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CourseServiceImpl implements CourseService {

    CourseDao courseDao= new CourseDaoImpl();
    public List<Course> findCourseList(){
        List<Course> courseList=courseDao.findCourseList();
        return courseList;
    }

    @Override
    public List<Course> findByCourseNameAndStatus(String courseName, String status) {
        List<Course> list = courseDao.findByCourseNameAndStatus(courseName, status);
        return list;
    }

    @Override
    public String saveCourseSalesInfo(Course course) {

        String dateFormart = DateUtils.getDateFormart();
        course.setCreate_time(dateFormart);
        course.setUpdate_time(dateFormart);
        course.setStatus(1);

        int row = courseDao.saveCourseSalesInfo(course);
        if(row>0){
            String s = StatusCode.SUCCESS.toString();
            return s;

        }else {
            String s = StatusCode.FAIL.toString();
            return s;

        }


    }

    @Override
    public Course findCourseById(int id) {
        return  courseDao.findCourseById(id);
    }

    @Override
    public String updateCourseSalesInfo(Course course) {
        int i = courseDao.updateCourseSalesInfo(course);
        if (i>0){
            //保存成功
            String result = StatusCode.SUCCESS.toString();
            return result;

        }else {
            String result = StatusCode.FAIL.toString();
            return result;

        }

    }

    @Override
    public Map<String, Integer> updateCourseStatus(Course course) {

        //调用Dao
        int i = courseDao.updateCourseStatus(course);
        Map<String,Integer> map=new HashMap<>();
        if(i>0){

            if(course.getStatus()==0){
                map.put("status",0);
            }else {
                map.put("status",1);
            }

        }

    return  map;

    }

}
